package org.xhy.retryer.demo;

import org.xhy.retryer.core.Retryer;
import org.xhy.retryer.core.RetryerBuilder;
import org.xhy.retryer.predicate.Predicates;
import org.xhy.retryer.strategy.TimeRetryerStrategy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-04-29 21:29
 */
public class Main {
    static volatile Boolean flag = true;
    public static void main(String[] args) throws InterruptedException {

        final Retryer retryer = RetryerBuilder.newBuilder()
                        .withRetryerStrategy(new TimeRetryerStrategy(3,1, TimeUnit.SECONDS))
                .retryerIfResult(Predicates.equalTo(true)) // 拦截返回 true 的结果并且重试
                .retryerLister(retryerStrategy -> {
                    System.out.println(retryerStrategy.getCount());
                })
                .build();
        new Thread(()->{
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            flag = false;
            System.out.println(flag);
        }).start();
        retryer.exec(() -> {
            System.out.println("hello xhy-retryer");
            System.out.println("我在里面"+flag);
            return flag;
        });
        flag = true;
        retryer.exec(() -> {
            System.out.println("hello xhy-retryer");
            System.out.println("我在里面"+flag);
            return flag;
        });

    }
}
